package com.ponhu.ea2.goodsCenter.provider.biz;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ponhu.ea2.entity.Material;
import com.ponhu.ea2.entity.PhBrand;
import com.ponhu.ea2.entity.PhMaterialPropertyValueMd;
import com.ponhu.ea2.goodsCenter.provider.mapper.PhCategoryMapper;
import com.ponhu.ea2.goodsCenter.provider.service.PhBrandService;
import com.ponhu.ea2.vo.PhCategoryLevel3Vo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 基础信息生成服务
 *
 * @author Wang
 * @date 2021/11/23 10:51 上午
 */
@Service
public class BaseInfoGenerateService {

    @Resource
    private PhCategoryMapper phCategoryMapper;
    @Resource
    private PhBrandService phBrandService;

    /**
     * 获取生成的物料名称
     * 生成规则如下：
     * a）箱包和其他分类物料名称规则：
     * 品牌名英文/中文 +颜色+材质+系列+尺码+适用人群+三级分类，若无系列、无型号或无大小等，则该字段不拼接。
     * <p>
     * b）钟表物料名称拼接规则：
     * 品牌英/中文+系列+表壳材质+机芯类型+适用人群+三级分类
     *
     * @return
     */
    public String createAndGetMaterialName(Material material, List<PhMaterialPropertyValueMd> materialPropertyValueMd) {
        StringBuilder builder = new StringBuilder();
        PhBrand phBrand = phBrandService.getById(material.getBrandId());
        if (phBrand != null) {
            builder.append(phBrand.getEnName()).append("/").append(phBrand.getCnName());
        }

        if (material.getCategoryId() == null) {
            return null;
        }

        PhCategoryLevel3Vo categoryLevel3Vo = phCategoryMapper.getCategoryLevel3VosByThirdCategoryId(material.getCategoryId());
        if (categoryLevel3Vo == null) {
            return null;
        }

        if(CollectionUtils.isNotEmpty(materialPropertyValueMd)){
            if ("钟表".equals(categoryLevel3Vo.getLevel1name())) {
                PhMaterialPropertyValueMd serialProperty = materialPropertyValueMd.stream().filter(f -> "系列".equals(f.getPropertyName())).findFirst().orElse(null);
                if (serialProperty != null) {
                    builder.append(" ").append(serialProperty.getPropertyValue());
                }

                PhMaterialPropertyValueMd watchcaseProperty = materialPropertyValueMd.stream().filter(f -> "表壳材质".equals(f.getPropertyName())).findFirst().orElse(null);
                if (watchcaseProperty != null) {
                    builder.append(" ").append(watchcaseProperty.getPropertyValue());
                }

                PhMaterialPropertyValueMd coreProperty = materialPropertyValueMd.stream().filter(f -> "机芯类型".equals(f.getPropertyName())).findFirst().orElse(null);
                if (coreProperty != null) {
                    builder.append(" ").append(coreProperty.getPropertyValue());
                }
            } else {
                PhMaterialPropertyValueMd colourProperty = materialPropertyValueMd.stream().filter(f -> "颜色".equals(f.getPropertyName())).findFirst().orElse(null);
                if (colourProperty != null) {
                    builder.append(" ").append(colourProperty.getPropertyValue());
                }

                PhMaterialPropertyValueMd materialProperty = materialPropertyValueMd.stream().filter(f -> "材质".equals(f.getPropertyName())).findFirst().orElse(null);
                if (materialProperty != null) {
                    builder.append(" ").append(materialProperty.getPropertyValue());
                }

                PhMaterialPropertyValueMd serialProperty = materialPropertyValueMd.stream().filter(f -> "系列".equals(f.getPropertyName())).findFirst().orElse(null);
                if (serialProperty != null) {
                    builder.append(" ").append(serialProperty.getPropertyValue());
                }

                PhMaterialPropertyValueMd sizeProperty = materialPropertyValueMd.stream().filter(f -> "尺码".equals(f.getPropertyName())).findFirst().orElse(null);
                if (sizeProperty != null) {
                    builder.append(" ").append(sizeProperty.getPropertyValue());
                }
            }
        }

        builder.append(" ").append(material.getApplyCrowd() == null ? "": material.getApplyCrowd()).append(" ").append(categoryLevel3Vo.getLevel3name());
        return builder.toString();
    }

}
